package edu.chl.johanwi.jsfproducts;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import edu.chl.johanwi.jpa.nbp.Exceptions.NonexistentEntityException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

/**
 *
 * @author Schtohan
 */
@ManagedBean
@SessionScoped
public class shoppingCart implements Serializable{

    private List<Product> productList;
    private int numberOfProducts;   
    private Product selected;

   
    /** Creates a new instance of shoppingCart */
    public shoppingCart() {
        
        productList = new ArrayList<Product>();
        numberOfProducts = productList.size();       
    }
   
    public Product getSelected() {
        return selected;
    }

    public void setSelected(Product selected) {
        this.selected = selected;
    }

    public int getNumberOfProducts() {
        return numberOfProducts;
    }

    public void setNumberOfProducts(int numberOfProducts) {
        this.numberOfProducts = numberOfProducts;
    }
    
    public String doAdd(){
        productList.add(selected);  
        numberOfProducts++;
        return "onAddProduct";
    }

    public List<Product> getProductList() {
        return productList;
    }

    public void setProductList(List<Product> productList) {
        this.productList = productList;
    }
    
    public int sortByPrice(Product p1, Product p2) {
        if(p1.getPrice() > p2.getPrice()){
            return 1;
        } else if(p1.getPrice() == p2.getPrice()){
            return 0;
        } else{
            return -1;           
        }
        
    }
    
    public void removeProduct()
    {
        productList.remove(selected);      
        numberOfProducts--;
    }
    
    
    
}
